package com.banana.manager.forest.controller.user;

import com.banana.manager.forest.entity.chemical.OutBean;
import com.banana.manager.forest.entity.user.LogBean;
import com.banana.manager.forest.entity.user.UserBean;
import com.banana.manager.forest.service.impl.user.LogServiceImpl;
import com.banana.manager.forest.service.impl.user.UserServiceImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserServiceImpl userService;

    @Resource
    private LogServiceImpl logService;

    @PostMapping("/login")
    public UserBean loginConfirm(String username, String password, HttpServletRequest request){
        List<OutBean> outBeanList = new ArrayList<>();
        HttpSession session = request.getSession();
        session.setAttribute("outList",outBeanList);
        UserBean confirmed = userService.userLogin(username,password);
        if(confirmed==null){
            return null;
        }else {
            String log = "用户"+confirmed.getUserName()+"在"+String.valueOf(LocalTime.now()).substring(0,5)+"登录系统";
            LogBean logBean = new LogBean(log, LocalDate.now());
            logService.addNewLog(logBean);
            return confirmed;
        }
    }

    @PostMapping("/addUser")
    public UserBean addUser(@RequestBody UserBean userBean){
        userService.addUser(userBean);
        return userBean;
    }

    @GetMapping("/deleteUser")
    public void deleteUser(Integer userId)
    {
        userService.deleteUserById(userId);
    }

    @GetMapping("/show")
    public UserBean findById(Integer userId){
        return userService.findById(userId);
    }

    @PostMapping("/updateUser")
    public UserBean updateUser(@RequestBody UserBean userBean){
        return userService.updateUser(userBean);
    }

    @GetMapping("/displayUser")
    public PageInfo<UserBean> displayUserList(Integer page, String userRole){
        return userService.selectUserByPage(page,userRole);
    }

    @GetMapping("/displayLog")
    public PageInfo<LogBean> displayLogList(Integer page, LocalDate start, LocalDate end){
        return logService.findAllByPage(page,start,end);
    }
}
